import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Color;
/*You can buy or not buy, none auctioning !*/
public class RailUI extends JPanel {
	private BoardUi board;
	private JButton btnBuy, btnNotBuy, btnPay;
	private JLabel label;
	private Player p;
	private Block[] bl;
	private Rail rail;
	private Rail[] railArray;
	private int currentRent;
	
	public RailUI(Player currentPlayer, Rail rail_, Block[] blk, BoardUi board_) {
		setBackground(new Color(245, 245, 245));
		
		this.p = currentPlayer;
		this.rail = rail_;
		this.bl=blk;
		this.railArray = new Rail[4];
		this.board=board_;
		
		setLayout(null);
		
		label = new JLabel("");
		label.setIcon(new ImageIcon(getClass().getResource(this.rail.getRailDeed())));
		label.setBounds(0, 0, 199, 200);
		label.setVisible(true);
		this.add(label);
		
		JLabel lblPriceAmount = new JLabel("");
		lblPriceAmount.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblPriceAmount.setBounds(62, 10, 63, 12);
		this.add(lblPriceAmount);
		
		btnBuy = new JButton("Bilin");
		btnBuy.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnBuy.setBounds(199, 68, 71, 23);
		btnBuy.setEnabled(false);
		btnBuy.setVisible(false);
		btnBuy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				buy();
			}
		});
		this.add(btnBuy);
		
		btnPay = new JButton("Renta");
		btnPay.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnPay.setBounds(199, 103, 71, 23);
		btnPay.setEnabled(false);
		btnPay.setVisible(false);
		btnPay.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				pay();
			}
		});
		this.add(btnPay);
		
		btnNotBuy = new JButton("Return");
		btnNotBuy.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnNotBuy.setBounds(199, 137, 100, 23);
		btnNotBuy.setEnabled(false);
		btnNotBuy.setVisible(false);
		btnNotBuy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				returnToGame();
			}
		});
		this.add(btnNotBuy);
		
		this.checkOwner();
		this.fillRailArray();
		this.getOwnedRailGroup();
	}

	public void buy() {
		if(board.getCurrentPlayer().getMoney()>= rail.getPrice()){
		rail.setOwner(p);
		p.subtractMoney(rail.getPrice());
		btnBuy.setEnabled(false); //you can only buy it one time
		btnBuy.setVisible(false);
		board.setCurrentPlayer();
		board.getPlayerPanelUI().updatePlayerMoney();
		board.getPlayerPanelUI().updateButtons(this.p.getPlayerCount(), this.p.getCurrentPosition());
		}else{
			JOptionPane.showMessageDialog(null,"You don't have enough Money to buy!");
		}
	}
	
	public void returnToGame(){
		board.getDiceUI().rolledDoubles();
	//	board.getGameButtonUI().enableEndTurnBtn();	
		setVisible(false);
	}
	
	public void pay(){
		if(board.getCurrentPlayer().getMoney()>= currentRent){
		p.subtractMoney(currentRent);
		rail.getOwner().addMoney(currentRent);
		btnPay.setEnabled(false);	//you can pay one time per turn
		btnPay.setVisible(false);
		btnNotBuy.setEnabled(true);
		btnNotBuy.setVisible(true);
		board.setCurrentPlayer();
		board.getPlayerPanelUI().updatePlayerMoney();
		}else{
			BankruptcyChecker bc = new BankruptcyChecker(p, bl);
			int worth=bc.checkWorth();
			
			if(worth>=currentRent){
				JOptionPane.showMessageDialog(null,"You don't have enough Money to pay rent, just mortgage your properties");
			}else{
				bc.transferAssetsPlayer();
				board.getCurrentPlayer().setActive(false);
				JOptionPane.showMessageDialog(null,"You don't have enough Money to pay rent, Out ka na!");
				btnNotBuy.setVisible(true);
				}	
			}
	}
	
	public void checkOwner(){
		if(this.rail.getOwner()==null){
			btnBuy.setEnabled(true);
			btnBuy.setVisible(true);
			btnNotBuy.setEnabled(true);
			btnNotBuy.setVisible(true);
		}else if(this.rail.getOwner()!=this.p){
			btnPay.setEnabled(true);
			btnPay.setVisible(true);
		}else if(this.rail.getOwner()==this.p){
			btnNotBuy.setEnabled(true);
			btnNotBuy.setVisible(true);
		}
	}
	
	public void fillRailArray() {
		int railCounter = 0;
		for(int i=0;i<this.bl.length;i++){
			if(this.bl[i] instanceof Rail){
				this.railArray[railCounter]=(Rail)bl[i];
				railCounter++;
			}
		}	
	}
	
	public void getOwnedRailGroup(){
			int ownedCounter = 0;
			
			for(int i=0; i<this.railArray.length; i++){
				if(this.railArray[i].getRailGroup()==this.rail.getRailGroup()){//match fist
					if(this.railArray[i].getOwner()==this.rail.getOwner() && this.rail.getOwner()!=null){//match second
						ownedCounter++;
					}
				}
			}
			if(4==ownedCounter){
				currentRent=this.rail.getRent3();
			}else if (3 ==ownedCounter){
				currentRent=this.rail.getRent2();
			}else if (2==ownedCounter){
				currentRent=this.rail.getRent1();
			}else if(1==ownedCounter){
				currentRent=this.rail.getRent0();
			}
		
	}	

}
